$(function () {
    initDg();
});

function initDg() {

    $('#dg').datagrid({
        width: '100%',
        height: 'auto',
        url: '/userInfo/list',
        fitColumns: true,
        autoRowHeight: false,
        striped: true,
        method: "GET",
        idField: "fdId",
        loadMsg: "正在加载数据...",
        pagination: true,
        rownumbers: true,
        pagePosition: 'bottom',
        pageSize: 50,
        pageList: [20, 30, 40, 50],
        showFooter: true,
        fit: true,
        queryParams: {
            name: $('#username').val()
        },
        columns: [[
            {field: 'fdId', title: 'ID', width: 100, align: 'center', checkbox: true},
            {
                field: 'fdNickname',
                width: 100,
                title: '用户昵称'
            },
            {
                field: 'fdType',
                width: 100,
                title: '用户类型',
                formatter: function (value, row, index) {
                    if (value === 1) {
                        return "普通用户";
                    }
                    else {
                        return "系统用户";
                    }
                }
            },
            {
                field: 'fdAvatarUrl',
                width: 50,
                title: '用户头像',
                formatter: function (value, row, index) {
                    return "<a onclick='showAvatar(\"" + value + "\")'>查看</a>";
                }
            },
            {
                field: 'fdSex',
                width: 40,
                title: '用户性别',
                formatter: function (value, row, index) {
                    if (value === 1) {
                        return "男";
                    }
                    else {
                        return "女";
                    }
                }
            },
            {
                field: 'fdGrade',
                width: 100,
                title: '用户等级'
            },
            {
                field: 'fdEmail',
                width: 100,
                title: '密保邮箱'
            },
            {
                field: 'fdMobile',
                width: 100,
                title: '用户手机'
            },
            {
                field: 'fdStatus',
                width: 50,
                title: '用户状态',
                formatter: function (value, row, index) {
                    if (value === 1) {
                        return "正常";
                    }
                    else {
                        return "禁用";
                    }
                }
            },
            {
                field: 'fdOver',
                title: '账户余额',
                width: 100
            },
            {
                field: 'fdBettingTotal',
                title: '投注总额',
                width: 100
            },
            {
                field: 'fdRechargeTotal',
                title: '充值总额',
                width: 100
            },
            {
                field: 'fdRecommendCode',
                width: 100,
                title: '推荐码'
            },
            {
                field: 'fdRoleName',
                width: 100,
                title: '用户角色'
            },
            {
                field: 'option',
                width: 100,
                title: '操作',
                align:'center',
                formatter:function (value,row,index) {
                    return "<a href='javascript:view()' class='easyui-linkbutton' onclick='view(\""+row.fdId+"\")'>查看</a>";
                }
            }
        ]],
        toolbar: '#tb'
    });
}

function view(fdId) {
    $('#win').window({
        width: 400,
        height: 700,
        modal: true,
        collapsible: false,
        minimizable: false,
        maximizable: false,
        resizable: true,
        iconCls: "icon-more",
        href: "/userInfo/view/" + fdId,
        title: "用户详情"

    });
}

function showAvatar(url) {
    $('#win').window({
        width: 400,
        height: 300,
        modal: true,
        collapsible: false,
        minimizable: false,
        maximizable: false,
        resizable: true,
        iconCls: "icon-add",
        content: "<img src='"+url+"' width='100%' height='100%'>",
        title: "头像"

    });

}

function addmoney() {
    var row = $('#dg').datagrid('getSelected');
    if (row === null) {
        $.messager.alert('提示', '请选中一行数据操作!', 'Warning');
    }
    var id = row.fdId;

    $('#win').window({
       width: 300,
       height: 150,
       modal: true,
       collapsible: false,
       minimizable: false,
       maximizable: false,
       resizable: true,
       iconCls: "icon-undo",
       href: "/userInfo/edit/" + id,
       title: "用户充值"

   });
}

function edit() {
    var row = $('#dg').datagrid('getSelected');
    if (row === null) {
        $.messager.alert('提示', '请选中一行数据操作!', 'Warning');
    }
    var id = row.fdId;

    $.post('/userInfo/updatePwd',{id:id},function (data) {
        if(data.code == '0'){
            $.messager.alert("结果",data.msg);
        }else{
            $.messager.alert("结果",data.msg);
        }
    })
    
    /*$('#win').window({
        width: 600,
        height: 400,
        modal: true,
        collapsible: false,
        minimizable: false,
        maximizable: false,
        resizable: true,
        iconCls: "icon-add",
        href: "/userInfo/edit/" + id,
        title: "编辑"

    });*/
}

function add() {
    $('#win').window({
        width: 600,
        height: 400,
        modal: true,
        collapsible: false,
        minimizable: false,
        maximizable: false,
        resizable: true,
        iconCls: "icon-add",
        href: "add.jsp",
        title: "添加"

    });
    changeUpload();
}

/*
function remove() {
    var row = $('#dg').datagrid('getSelections');
    if (row.length === 0) {
        $.messager.alert('提示', '请选中一行数据操作!', 'Warning');
    } else {
        var ids = new Array();
        $.each(row, function (i, row) {
            ids[i] = row['fdId'];
        });
        $.messager.confirm('提示', '确认删除该条记录?', function (r) {
            if (r) {
                console.log(ids);
                $.ajax({
                    type: 'POST',
                    url: '/userInfo/remove',
                    data: {"ids": ids},
                    success: function (r) {
                        if (r.code == 0) {
                            $.messager.alert('提示', r.msg, 'info');
                            reLoad();
                            clearSelections();
                        } else {
                            $.messager.alert('提示', r.msg, 'Warning');
                        }
                    }
                });
            }
        });
    }
}
*/

function submitForm() {
    $('#ff').form('submit', {
        onSubmit: function () {
            return $(this).form('enableValidation').form('validate');
        },
        success: function (data) {
            var r = JSON.parse(data);
            if (r.code == 0) {
                $.messager.alert("提示", r.msg, "info");
                closeWin();
                reLoad();
                clearSelections();
            } else {
                $.messager.alert("提示", r.msg, "Warning")
            }
        }
    });
}

function clearForm() {
    $('#ff').form('clear');
}

function reLoad() {
    //$('#dg').datagrid('reload');//刷新
    initDg();
}

function closeWin() {
    $("#win").window("close");
}

function clearSelections() {
    $("#dg").datagrid("clearSelections");
}

function changeUpload() {
    console.log($("#fdAvatarUrl").filebox());
    $("#fdAvatarUrl").filebox({
        buttonText: '选择文件',
        buttonAlign: 'right'
    });
}
